package function;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author wd07098
 *
 */
public class WriteOrReadFile
{
    /**
     * @param filePath
     * @param value
     */
    public static void writeIntoFile(String filePath, String value)
    {
        File file=new File(filePath);//"D:/SuperGo.txt"
        
        if(file.exists() && file.isFile())
        {
            file.delete();
            try
	    {
		file.createNewFile();
	    } catch (IOException e)
	    {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
        }
        else
        {
            try
	    {
		file.createNewFile();
	    } catch (IOException e)
	    {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
        }

        FileOutputStream out;
	try
	{
	    out = new FileOutputStream(file,true);
	    out.write(value.getBytes("utf-8"));
	} catch (FileNotFoundException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (UnsupportedEncodingException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
    
    /**
     * @param filePath
     * @return
     */
    public static String readValueFormFile(String filePath)
    {
	StringBuffer sb = new StringBuffer();
	File file = new File(filePath);
	if (!file.exists() || file.isDirectory())
	{
	    System.out.println("文件不存在！");
	}

	FileInputStream fis;
	try
	{
	    fis = new FileInputStream(file);
	    byte[] buf = new byte[1024];
	    while ((fis.read(buf)) != -1)
	    {
		sb.append(new String(buf));
		buf = new byte[1024];// 
	    }
	} catch (IOException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return sb.toString();
    }
    

}
